package com.example.controller;

import com.example.common.Result;
import com.example.entity.VideoHistory;
import com.example.service.VideoHistoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 历史记录视频前端接口
 */
@RestController
@RequestMapping("/videoHistory")
public class VideoHistoryController {

    @Resource
    private VideoHistoryService videoHistoryService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody VideoHistory videoHistory) {
        List<VideoHistory> list = videoHistoryService.selectAll(videoHistory);

        if (list != null && !list.isEmpty()) {
            // 如果List有结果，则退出
            return Result.success("List already contains data. Exiting without adding.");
        } else {
            // 否则执行添加操作
            videoHistoryService.add(videoHistory);
            return Result.success("Data added successfully.");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        videoHistoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        videoHistoryService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody VideoHistory videoHistory) {
        videoHistoryService.updateById(videoHistory);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        VideoHistory videoHistory = videoHistoryService.selectById(id);
        return Result.success(videoHistory);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(VideoHistory videoHistory ) {
        List<VideoHistory> videoHistoryList = videoHistoryService.selectAll(videoHistory);
        return Result.success(videoHistoryList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(VideoHistory videoHistory,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<VideoHistory> pageInfo = videoHistoryService.selectPage(videoHistory, pageNum, pageSize);
        return Result.success(pageInfo);
    }

}
